package cn.com.dhc.zzz.back.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bbs.ba.base.BAAction;
import cn.bbs.ba.base.UserInfo;
import cn.bbs.ba.factory.BABeanFactory;
import cn.bbs.ba.html.ZPaiConstant;
import cn.bbs.ba.mi.BAMima;
import cn.bbs.ba.util.DtoUtil;
import cn.com.zzz.dao.PPUserInfoDao;

/**
 * 用户自由注册
 */
public class PKUserInfoUpdateAction extends BAAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		super.execute(mapping, form, request, response);
		UserInfo userInfo = new UserInfo();
		DtoUtil.formToDto(form, userInfo);
		if (StringUtils.isEmpty(userInfo.getUpdateFlag())) {
			return mapping.findForward("comError");
		}
		PPUserInfoDao userInfoDao = (PPUserInfoDao)BABeanFactory.getBean("userInfoDao");
		// 更新基本信息
		int insertCnt = 0;
		
		// 修改个人资料
		if ("userInfo".equals(userInfo.getUpdateFlag())) {
			super.userInfo.setEmail(userInfo.getEmail());
			super.userInfo.setMobile(userInfo.getMobile());
		}
		// 修改密码
		else if ("pwdUpdate".equals(userInfo.getUpdateFlag())) {
			UserInfo userInfoSearch = new UserInfo();
			userInfoSearch.setAlias(super.userInfo.getAlias());
			userInfoSearch.setPASSPORT(BAMima.encry(super.userInfo.getAlias() + userInfo.getOldPwd()));
			if(userInfoDao.getUser(userInfoSearch) == null) {
				// 建立登陆SESSION
				request.setAttribute("showValue", ZPaiConstant.disPlay("输入旧密码不正确"));
				// 会到主页 手机验证取消
				return mapping.findForward("success");
			}
			super.userInfo.setPASSPORT(BAMima.encry(super.userInfo.getAlias() + userInfo.getNewPwd()));
		}

		insertCnt = userInfoDao.updateUserInfoByUserNo(super.userInfo);

		
		if (insertCnt == 1) {
			// 建立登陆SESSION
			request.setAttribute("showValue", ZPaiConstant.disPlay("修改成功！"));
			// 会到主页 手机验证取消
			return mapping.findForward("success");
		}
		else {
			request.setAttribute("showValue", ZPaiConstant.disPlay("修改失败！"));
			// 用户已经存在
			return mapping.findForward("success");
		}
	}

}
